package com.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.alibaba.fastjson.JSON;
import com.dao.RecruitDao;
import com.opensymphony.xwork2.ActionContext;
import com.po.AllRecruitInfoVO;
import com.po.RecruitDetailInfoPO;
import com.po.RecruitPO;
@Controller
public class RecruitAction {
	@Autowired
	private RecruitDao recruitDao;
	private List<RecruitPO> recruitPOs;
	private String imageFileName;
	private File image;
	private RecruitPO recruitPO;
	private RecruitDetailInfoPO detailInfoPO;
	private AllRecruitInfoVO allRecruitInfoVO;
	/**
	 * 展示招聘信息
	 */
	public String show(){
		recruitPOs =  recruitDao.findRecruitInfo();
		
		return "show";
	}
	
	/**
	 * 上传图片
	 */
	public String upImage(){
		try {
			//给上传的图片重新起名字
			String name = imageFileName;
			long date = System.currentTimeMillis();
			String NewName = date + name.substring(name.lastIndexOf(".")); 
			
			//获取ServletContext对象
			ServletContext context = ServletActionContext.getServletContext();
			
			//	给服务器中上传文件
			FileInputStream fis = new FileInputStream(image);
			int t = 0;
			byte[] bys = new byte[1024];
			FileOutputStream fos = new FileOutputStream(context.getRealPath("/") + "recruit_image/" + NewName);
			while((t = fis.read(bys)) != -1){
				fos.write(bys, 0, t);
			}
			//关闭流
			fis.close();
			fos.close();
			//服务器响应的数据
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setContentType("text/html;charset=utf-8");
			response.getWriter().write(NewName);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	 return null;
	}
	/**
	 * 跳转到修改界面,回显数据
	 * @return
	 */
	public String preUpdate(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String id = request.getParameter("id");
		recruitPO = recruitDao.findRecruitInfoById(id);
		detailInfoPO = recruitDao.findRecruitDetailInfoById(id);
		ActionContext.getContext().put("id", id);
		
		return "preUpdate";
	}
	
	/**
	 * 修改招聘信息
	 * @return
	 */
	public String update(){
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		Map<String, String> map = new HashMap<String, String>();
		int id = Integer.parseInt(request.getParameter("id"));
		allRecruitInfoVO.setRecruit_id(id);
		try {
			recruitDao.updateInfo(allRecruitInfoVO);
			recruitDao.updateDetailInfo(allRecruitInfoVO);
			map.put("code", "EC001");
		} catch (Exception e) {
			map.put("code", "EC002");
		}
		try {
			String msg = JSON.toJSONString(map);
			response.setContentType("text/html;charset=utf-8");
			response.getWriter().write(msg);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * 跳转到添加页面
	 */
	public String preAdd(){
		return "preAdd";
	}
	
	
	
	/**
	 * 添加招聘信息
	 * @return
	 */
	public String add(){
		HttpServletResponse response = ServletActionContext.getResponse();
		HashMap<String, String> map = new HashMap<String, String>();
		try {
			recruitDao.addRecruitInfo(allRecruitInfoVO);
			recruitDao.addRecruit_fid(allRecruitInfoVO.getRecruit_id());
			recruitDao.addRecruitDetailInfo(allRecruitInfoVO);
			map.put("code", "EC001");
		} catch (Exception e) {
			map.put("code", "EC002");
		}
		try {
			String msg = JSON.toJSONString(map);
			response.setContentType("text/html;charset=utf-8");
			response.getWriter().write(msg);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 删除招聘信息
	 * @return
	 */
	public String delete(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String id = request.getParameter("id");
		recruitDao.deleteRecruitInfo(id);
		recruitDao.deleteDetailInfoById(id);
		return "after_delete";
	}
	
	
	public RecruitPO getRecruitPO() {
		return recruitPO;
	}

	public void setRecruitPO(RecruitPO recruitPO) {
		this.recruitPO = recruitPO;
	}

	public String getImageFileName() {
		return imageFileName;
	}

	public void setImageFileName(String imageFileName) {
		this.imageFileName = imageFileName;
	}

	public File getImage() {
		return image;
	}

	public void setImage(File image) {
		this.image = image;
	}

	public List<RecruitPO> getRecruitPOs() {
		return recruitPOs;
	}

	public void setRecruitPOs(List<RecruitPO> recruitPOs) {
		this.recruitPOs = recruitPOs;
	}

	public AllRecruitInfoVO getAllRecruitInfoVO() {
		return allRecruitInfoVO;
	}

	public void setAllRecruitInfoVO(AllRecruitInfoVO allRecruitInfoVO) {
		this.allRecruitInfoVO = allRecruitInfoVO;
	}

	public RecruitDetailInfoPO getDetailInfoPO() {
		return detailInfoPO;
	}

	public void setDetailInfoPO(RecruitDetailInfoPO detailInfoPO) {
		this.detailInfoPO = detailInfoPO;
	}
	
	
	
}
